﻿/* 
 * Since 2008 - 2012
 */

package com.hh.bss.oa.dao.impl;

import java.util.*;

import com.hh.bss.base.*;
import com.hh.bss.common.MapObjectRowMapper;
import com.hh.bss.common.page.*;
import com.hh.bss.common.page.impl.*;
import com.hh.bss.oa.dao.*;
import com.hh.bss.oa.entity.*;
import com.hh.bss.oa.service.*;
import com.hh.bss.oa.vo.query.*;
import com.hh.bss.util.*;



import static com.hh.bss.util.ObjectUtils.*;

import org.springframework.stereotype.Repository;

@Repository
public class OaEmailSendDaoImpl extends BaseHibernateDao<OaEmailSend, java.lang.Integer> implements OaEmailSendDao<OaEmailSend, java.lang.Integer> {

	public Class getEntityClass() {
		return OaEmailSend.class;
	}

	public Page findPage(OaEmailSendQuery query) {
		// XsqlBuilder syntax,please see
		// http://code.google.com/p/rapid-xsqlbuilder
		// [column]为字符串拼接, {column}为使用占位符.
		// [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接
		// [column] 为PageRequest的属性

		Class[] os = { OaEmailSendQuery.class };
		Class property = org.springframework.beans.BeanUtils.findPropertyType(query.getField_type(), os);
		if (property != null) {
			if ("java.lang.String".equals(property.getName())) {

				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), query.getQuery());

			} else if ("java.lang.Integer".equals(property.getName())) {

				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseInteger(query.getQuery(), 0));

			} else if ("java.lang.Double".equals(property.getName())) {
				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), StringHelper.parseDouble(query.getQuery(), 0));
			} else if ("java.util.Date".equals(property.getName())) {
				com.hh.bss.util.BeanUtils.setProperty(query, query.getField_type(), DateHelper.toDate(query.getQuery(), DateHelper.DEFAULT_DATE_FORMAT));
			}
		}
		// 生成sql2的原因是为了不喜欢使用xsqlbuilder的同学，请修改生成器模板，删除本段的生成
		StringBuilder sql2 = new StringBuilder("select t from OaEmailSend t where 1=1 ");
		if (isNotEmpty(query.getId())) {
			sql2.append(" and  t.id = :id ");
		}
		if (isNotEmpty(query.getCallFrom())) {
			sql2.append(" and  t.callFrom like :callFrom ");
			query.setCallFrom("%" + query.getCallFrom() + "%");

		}
		if (isNotEmpty(query.getCallId())) {
			sql2.append(" and  t.callId = :callId ");
		}

		if (isNotEmpty(query.getSubject())) {
			sql2.append(" and  t.subject like :subject ");
			query.setSubject("%" + query.getSubject() + "%");

		}
		if (isNotEmpty(query.getSender())) {
			sql2.append(" and  t.sender like :sender ");
			query.setSender("%" + query.getSender() + "%");

		}
		if (isNotEmpty(query.getReciever())) {
			sql2.append(" and  t.reciever like :reciever ");
			query.setReciever("%" + query.getReciever() + "%");

		}
		if (isNotEmpty(query.getContent())) {
			sql2.append(" and  t.content like :content ");
			query.setContent("%" + query.getContent() + "%");

		}
		if (isNotEmpty(query.getSortColumns())) {
			sql2.append(" order by :sortColumns ");
		}

		return pageQuery(sql2.toString(), query);
	}

	@Override
	public boolean checkJobRecomSender(OaEmailSend oaEmailSend) {
		List values = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from contract_job_cv_jd  a where a.type = 2 and a.job_id = ? and a.cv_id = ? and a.creator != ? ");
		values.add(oaEmailSend.getJobId());
		values.add(oaEmailSend.getCvId());
		values.add(oaEmailSend.getCreator());

		List l = this.getJdbcDao().findByQuery(sql.toString(), values.toArray(), new MapObjectRowMapper());
		if (l != null && l.size() > 0) {
			return true;
		}
		return false;
	}

}
